
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      <link rel="icon" href="../../../../static/images/favicon.png">
      <meta name="generator" content="mkdocs-1.3.0, mkdocs-material-8.2.8">
    
    
      
        <title>README CN - WL4G DOCS</title>
      
    
    
      <link rel="stylesheet" href="../../../../assets/stylesheets/main.644de097.min.css">
      
        
        <link rel="stylesheet" href="../../../../assets/stylesheets/palette.e6a45f82.min.css">
        
      
    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../../../../static/css/util.css">
    
    <script>__md_scope=new URL("../../../..",location),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="" data-md-color-accent="">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#1" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
      <div data-md-component="outdated" hidden>
        <aside class="md-banner md-banner--warning">
          
        </aside>
      </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../../../.." title="WL4G DOCS" class="md-header__button md-logo" aria-label="WL4G DOCS" data-md-component="logo">
      
  <img src="../../../../static/images/mylogo.jpeg" alt="logo">

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            WL4G DOCS
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              README CN
            
          </span>
        </div>
      </div>
    </div>
    
      <form class="md-header__option" data-md-component="palette">
        
          
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme="default" data-md-color-primary="" data-md-color-accent=""  aria-label="Switch to dark mode"  type="radio" name="__palette" id="__palette_1">
          
            <label class="md-header__button md-icon" title="Switch to dark mode" for="__palette_2" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17 6H7c-3.31 0-6 2.69-6 6s2.69 6 6 6h10c3.31 0 6-2.69 6-6s-2.69-6-6-6zm0 10H7c-2.21 0-4-1.79-4-4s1.79-4 4-4h10c2.21 0 4 1.79 4 4s-1.79 4-4 4zM7 9c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/></svg>
            </label>
          
        
          
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme="slate" data-md-color-primary="" data-md-color-accent=""  aria-label="Switch to light mode"  type="radio" name="__palette" id="__palette_2">
          
            <label class="md-header__button md-icon" title="Switch to light mode" for="__palette_1" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17 7H7a5 5 0 0 0-5 5 5 5 0 0 0 5 5h10a5 5 0 0 0 5-5 5 5 0 0 0-5-5m0 8a3 3 0 0 1-3-3 3 3 0 0 1 3-3 3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>
            </label>
          
        
      </form>
    
    
      <div class="md-header__option">
        <div class="md-select">
          
          <button class="md-header__button md-icon" aria-label="Select language">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m12.87 15.07-2.54-2.51.03-.03A17.52 17.52 0 0 0 14.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7 1.62-4.33L19.12 17h-3.24z"/></svg>
          </button>
          <div class="md-select__inner">
            <ul class="md-select__list">
              
                <li class="md-select__item">
                  <a href="/en/" hreflang="en" class="md-select__link">
                    English
                  </a>
                </li>
                
                <li class="md-select__item">
                  <a href="/zh/" hreflang="zh" class="md-select__link">
                    简体中文
                  </a>
                </li>
                
            </ul>
          </div>
        </div>
      </div>
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
        </button>
      </nav>
      
        <div class="md-search__suggest" data-md-component="search-suggest"></div>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            Initializing search
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="Tabs" data-md-component="tabs">
  <div class="md-tabs__inner md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../../.." class="md-tabs__link">
        Getting Started
      </a>
    </li>
  

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


  

<nav class="md-nav md-nav--primary md-nav--lifted md-nav--integrated" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../../../.." title="WL4G DOCS" class="md-nav__button md-logo" aria-label="WL4G DOCS" data-md-component="logo">
      
  <img src="../../../../static/images/mylogo.jpeg" alt="logo">

    </a>
    WL4G DOCS
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" data-md-state="indeterminate" type="checkbox" id="__nav_1" checked>
      
      
      
      
        <label class="md-nav__link" for="__nav_1">
          Getting Started
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="Getting Started" data-md-level="1">
        <label class="md-nav__title" for="__nav_1">
          <span class="md-nav__icon md-icon"></span>
          Getting Started
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../.." class="md-nav__link">
        Introduction
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../../ABOUT_CN/" class="md-nav__link">
        About
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                


  <h1>README CN</h1>

<ul>
<li>
<p><a href="https://blogs.wl4g.com/archives/650">另一篇: 基于主机离线部署 MySQL MGR 高可用生产集群</a></p>
</li>
<li>
<p>准备工作</p>
</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1"></a><a href="#__codelineno-0-1"><span class="linenos" data-linenos=" 1 "></span></a><span class="c1"># -----临时禁用所有接口 ipv6. -----</span>
<a id="__codelineno-0-2" name="__codelineno-0-2"></a><a href="#__codelineno-0-2"><span class="linenos" data-linenos=" 2 "></span></a><span class="k">for</span> f <span class="k">in</span> <span class="k">$(</span>ls /proc/sys/net/ipv6/conf/*/disable_ipv6<span class="k">)</span><span class="p">;</span> <span class="k">do</span> sudo su - root -c <span class="s2">&quot;echo 1 &gt;</span><span class="nv">$f</span><span class="s2">&quot;</span><span class="p">;</span> <span class="k">done</span>
<a id="__codelineno-0-3" name="__codelineno-0-3"></a><a href="#__codelineno-0-3"><span class="linenos" data-linenos=" 3 "></span></a><span class="c1"># 再次查看所有接口 ipv6 启用状态</span>
<a id="__codelineno-0-4" name="__codelineno-0-4"></a><a href="#__codelineno-0-4"><span class="linenos" data-linenos=" 4 "></span></a>cat /proc/sys/net/ipv6/conf/*/disable_ipv6
<a id="__codelineno-0-5" name="__codelineno-0-5"></a><a href="#__codelineno-0-5"><span class="linenos" data-linenos=" 5 "></span></a>
<a id="__codelineno-0-6" name="__codelineno-0-6"></a><a href="#__codelineno-0-6"><span class="linenos" data-linenos=" 6 "></span></a><span class="c1"># ----- 永久禁用所有接口 ipv6. -----</span>
<a id="__codelineno-0-7" name="__codelineno-0-7"></a><a href="#__codelineno-0-7"><span class="linenos" data-linenos=" 7 "></span></a>sudo cat <span class="s">&lt;&lt;-&#39;EOF&#39; &gt;/etc/sysctl.d/99-ipv6-disable.conf</span>
<a id="__codelineno-0-8" name="__codelineno-0-8"></a><a href="#__codelineno-0-8"><span class="linenos" data-linenos=" 8 "></span></a><span class="s">net.ipv6.conf.all.disable_ipv6=1</span>
<a id="__codelineno-0-9" name="__codelineno-0-9"></a><a href="#__codelineno-0-9"><span class="linenos" data-linenos=" 9 "></span></a><span class="s">net.ipv6.conf.default.disable_ipv6=1</span>
<a id="__codelineno-0-10" name="__codelineno-0-10"></a><a href="#__codelineno-0-10"><span class="linenos" data-linenos="10 "></span></a><span class="s">EOF</span>
<a id="__codelineno-0-11" name="__codelineno-0-11"></a><a href="#__codelineno-0-11"><span class="linenos" data-linenos="11 "></span></a>
<a id="__codelineno-0-12" name="__codelineno-0-12"></a><a href="#__codelineno-0-12"><span class="linenos" data-linenos="12 "></span></a><span class="c1"># 禁用每个接口 ipv6 (虽然设置了 net.ipv6.conf.all.disable_ipv6=1</span>
<a id="__codelineno-0-13" name="__codelineno-0-13"></a><a href="#__codelineno-0-13"><span class="linenos" data-linenos="13 "></span></a><span class="c1"># 但默认对每个接口, 如: net.ipv6.conf.enp3s0.disable_ipv6=0) 还是启用的.</span>
<a id="__codelineno-0-14" name="__codelineno-0-14"></a><a href="#__codelineno-0-14"><span class="linenos" data-linenos="14 "></span></a>
<a id="__codelineno-0-15" name="__codelineno-0-15"></a><a href="#__codelineno-0-15"><span class="linenos" data-linenos="15 "></span></a><span class="k">for</span> i <span class="k">in</span> <span class="k">$(</span>ifconfig <span class="p">|</span> awk -F<span class="s1">&#39;[ :]+&#39;</span> <span class="s1">&#39;!NF{if(eth!=&quot;&quot;&amp;&amp;ip==&quot;&quot;)print eth;eth=ip4=&quot;&quot;}/^[^ ]/{eth=$1}/inet addr:/{ip=$4}&#39;</span><span class="k">)</span><span class="p">;</span> <span class="k">do</span> <span class="nb">echo</span> <span class="s2">&quot;net.ipv6.conf.</span><span class="nv">$i</span><span class="s2">.disable_ipv6=1&quot;</span> &gt;&gt;/etc/sysctl.d/99-ipv6-disable.conf<span class="p">;</span> <span class="k">done</span>
<a id="__codelineno-0-16" name="__codelineno-0-16"></a><a href="#__codelineno-0-16"><span class="linenos" data-linenos="16 "></span></a>
<a id="__codelineno-0-17" name="__codelineno-0-17"></a><a href="#__codelineno-0-17"><span class="linenos" data-linenos="17 "></span></a>sudo sysctl -p
</code></pre></div>
<h2 id="1">1. 部署单节点伪集群<a class="headerlink" href="#1" title="Permanent link">&para;</a></h2>
<blockquote>
<p>创建的三个节点分布在单台宿主机上，所有操作均在1台宿主机上，不存在在跨主机通信的情况 (使用 subnet 网络端口转发)，适合快速测试。</p>
</blockquote>
<table>
<thead>
<tr>
<th>Container IP</th>
<th>Container Host</th>
<th>MGR Role</th>
</tr>
</thead>
<tbody>
<tr>
<td>172.8.8.111</td>
<td>rds-mgr-0</td>
<td>Group Repplication Master</td>
</tr>
<tr>
<td>172.8.8.112</td>
<td>rds-mgr-1</td>
<td>Group Repplication Slave1</td>
</tr>
<tr>
<td>172.8.8.113</td>
<td>rds-mgr-2</td>
<td>Group Repplication Salve2</td>
</tr>
</tbody>
</table>
<ul>
<li>1.1 准备配置</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] /mnt/disk1/mysql-pseudo/conf1/my.cnf</summary>
<code><pre>
# 下载配置模版
sudo curl -sSL -o /tmp/my.cnf.tpl https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/hosts-mysql-mgr-deployment/resources/my.cnf.tpl

# 生成3个节点的配置（***按实际自行修改***）
export mysqlIp0='172.8.8.111'
export mysqlIp1='172.8.8.112'
export mysqlIp2='172.8.8.113'
export seeds="$mysqlIp0:23366,$mysqlIp1:23366,$mysqlIp2:23366"
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp0/g" | sed 's/SERVER_ID/1/g' | sed "s/SEEDS/$seeds/g" > /tmp/my0.cnf
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp1/g" | sed 's/SERVER_ID/2/g' | sed "s/SEEDS/$seeds/g" > /tmp/my1.cnf
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp2/g" | sed 's/SERVER_ID/3/g' | sed "s/SEEDS/$seeds/g" > /tmp/my2.cnf

# 创建本地目录
sudo \rm -rf /mnt/disk1/mysql/mysql-pseudo/*
sudo mkdir -p /mnt/disk1/mysql-pseudo/{node0/conf,node0/data,node1/conf,node1/data,node2/conf,node2/data}

# 拷贝多个伪节点
sudo cp /tmp/my0.cnf /mnt/disk1/mysql-pseudo/node0/conf/my.cnf
sudo cp /tmp/my1.cnf /mnt/disk1/mysql-pseudo/node1/conf/my.cnf
sudo cp /tmp/my2.cnf /mnt/disk1/mysql-pseudo/node2/conf/my.cnf

# 授权
sudo chmod -R 755 /mnt/disk1/mysql-pseudo/
</pre></code>
</details></p>

<ul>
<li>1.2 创建 mysql 伪集群容器节点</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] run mysql pseudo containers</summary>
<code><pre>
# 拉取镜像
docker pull mysql:5.7.30

# 创建虚拟网络(仅限本机, 跨宿主机生产环境应使用 k8s 部署)
docker network create --subnet=172.8.8.0/24 mysqlnet

# 若已创建则重置
docker stop mysql0 mysql1 mysql2
docker rm mysql0 mysql1 mysql2

# mysql node0
docker run -d \
--name mysql0 \
--network mysqlnet \
--ip $mysqlIp0 \
--hostname rds-mgr-0 \
--restart no \
-p 33060:3306 \
-v /mnt/disk1/mysql-pseudo/node0/conf/:/etc/mysql/conf.d/ \
-v /mnt/disk1/mysql-pseudo/node0/data/:/var/lib/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30

# mysql node1
docker run -d \
--name mysql1 \
--network mysqlnet \
--ip $mysqlIp1 \
--hostname rds-mgr-1 \
--restart no \
-p 33061:3306 \
-v /mnt/disk1/mysql-pseudo/node1/conf/:/etc/mysql/conf.d/ \
-v /mnt/disk1/mysql-pseudo/node1/data/:/var/lib/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30

# mysql node2
docker run -d \
--name mysql2 \
--network mysqlnet \
--ip $mysqlIp2 \
--hostname rds-mgr-2 \
--restart no \
-p 33062:3306 \
-v /mnt/disk1/mysql-pseudo/node2/conf/:/etc/mysql/conf.d/ \
-v /mnt/disk1/mysql-pseudo/node2/data/:/var/lib/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30

# 查看伪集群容器列表
docker ps

# 客户端连接测试
export localIp=$(ifconfig|grep -A 4 -E '^em*|^eno*|^enp*|^ens*|^eth*|^wlp*'|grep 'inet'|awk '{print $2}'|head -1 2>/dev/null)
mysql -h$localIp -uroot -P33060 -p123456
</pre></code>
</details></p>

<ul>
<li>
<p>1.3 配置 MGR 及引导启动</p>
</li>
<li>
<p><a href="https://blogs.wl4g.com/archives/650">同理参见: 基于主机离线部署 MySQL MGR 高可用生产集群#7.1</a></p>
</li>
<li>
<p>或使用工具 <a href="https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/docker-mysql-mgr-simple-deployment/resources/mgrctl.sh">mgrctl</a> 操作
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1"></a><a href="#__codelineno-1-1"><span class="linenos" data-linenos="1 "></span></a>sudo curl -sSL -o /bin/mgrctl https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/docker-mysql-mgr-simple-deployment/resources/mgrctl.sh
</code></pre></div></p>
</li>
</ul>
<h2 id="2">2. 部署跨节点生产集群<a class="headerlink" href="#2" title="Permanent link">&para;</a></h2>
<blockquote>
<p>创建的三个节点分布在三台宿主机上，所有操作分别在三台不同的宿主机上，存在跨主机通信的情况 (使用 host 网络)，适合简单生产环境。</p>
</blockquote>
<table>
<thead>
<tr>
<th>Host IP</th>
<th>MGR Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>10.0.0.161</td>
<td>Group Repplication Master</td>
<td>node0 mgr</td>
</tr>
<tr>
<td>10.0.0.162</td>
<td>Group Repplication Slave1</td>
<td>node1 mgr</td>
</tr>
<tr>
<td>10.0.0.163</td>
<td>Group Repplication Salve2</td>
<td>node2 mgr</td>
</tr>
</tbody>
</table>
<ul>
<li>
<p>2.1 创建 mysql 集群容器节点</p>
</li>
<li>
<p>2.2.1 准备配置</p>
</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] /mnt/disk1/mysql/conf.d/my.cnf</summary>
<code><pre>
# 下载配置模版
sudo curl -sSL -o /tmp/my.cnf.tpl https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/hosts-mysql-mgr-deployment/resources/my.cnf.tpl

# 生成3个节点的配置（***按实际自行修改***）
export mysqlIp0='10.0.0.161'
export mysqlIp1='10.0.0.162'
export mysqlIp2='10.0.0.163'
export seeds="$mysqlIp0:23366,$mysqlIp1:23366,$mysqlIp2:23366"
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp0/g" | sed 's/SERVER_ID/1/g' | sed "s/SEEDS/$seeds/g" > /tmp/my0.cnf
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp1/g" | sed 's/SERVER_ID/2/g' | sed "s/SEEDS/$seeds/g" > /tmp/my1.cnf
sudo cat /tmp/my.cnf.tpl | sed "s/HOST/$mysqlIp2/g" | sed 's/SERVER_ID/3/g' | sed "s/SEEDS/$seeds/g" > /tmp/my2.cnf

# 分发配置
sudo cp -Rf /tmp/my0.cnf /mnt/disk1/mysql/conf.d/my.cnf
sudo ssh root@$mysqlIp1 'mkdir -p /mnt/disk1/mysql/conf.d/'; sudo scp /tmp/my1.cnf $mysqlIp1:/mnt/disk1/mysql/conf.d/my.cnf
sudo ssh root@$mysqlIp2 'mkdir -p /mnt/disk1/mysql/conf.d/'; sudo scp /tmp/my2.cnf $mysqlIp2:/mnt/disk1/mysql/conf.d/my.cnf
</pre></code>
</details></p>

<ul>
<li>2.1.2 在宿主机 node0 上执行</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] run node0 mgr container</summary>
<code><pre>
docker pull mysql:5.7.30

# 如果已创建过，则需重置
docker stop $(docker ps|grep 'mysql'|awk '{print $1}')
docker rm $(docker ps -a|grep 'mysql'|awk '{print $1}')

sudo \rm -rf /mnt/disk1/mysql/data/; mkdir -p /mnt/disk1/mysql/data

docker run -d \
--name mysql0 \
--hostname mysql0 \
--network host \
--restart always \
-v /mnt/disk1/mysql/conf.d/:/etc/mysql/conf.d \
-v /mnt/disk1/mysql/data/:/var/lib/mysql/ \
-v /mnt/disk1/log/mysql/:/var/log/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30
</pre></code>
</details></p>

<ul>
<li>2.1.3 在宿主机 node1 上执行</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] run node1 mgr container</summary>
<code><pre>
docker pull mysql:5.7.30

# 如果已创建过，则需重置
docker stop $(docker ps|grep 'mysql'|awk '{print $1}')
docker rm $(docker ps -a|grep 'mysql'|awk '{print $1}')

sudo \rm -rf /mnt/disk1/mysql/data/; mkdir -p /mnt/disk1/mysql/data

docker run -d \
--name mysql1 \
--hostname mysql1 \
--network host \
--restart always \
-v /mnt/disk1/mysql/conf.d/:/etc/mysql/conf.d \
-v /mnt/disk1/mysql/data/:/var/lib/mysql/ \
-v /mnt/disk1/log/mysql/:/var/log/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30
</pre></code>
</details></p>

<ul>
<li>2.1.4 在宿主机 node2 上执行</li>
</ul>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] run node2 mgr container</summary>
<code><pre>
docker pull mysql:5.7.30

# 如果已创建过，则需重置
docker stop $(docker ps|grep 'mysql'|awk '{print $1}')
docker rm $(docker ps -a|grep 'mysql'|awk '{print $1}')

sudo \rm -rf /mnt/disk1/mysql/data/; mkdir -p /mnt/disk1/mysql/data

docker run -d \
--name mysql2 \
--hostname mysql2 \
--network host \
--restart always \
-v /mnt/disk1/mysql/conf.d/:/etc/mysql/conf.d \
-v /mnt/disk1/mysql/data/:/var/lib/mysql/ \
-v /mnt/disk1/log/mysql/:/var/log/mysql/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
mysql:5.7.30
</pre></code>
</details></p>

<ul>
<li>先简单测试一下每个实例</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1"></a><a href="#__codelineno-2-1"><span class="linenos" data-linenos="1 "></span></a><span class="c1"># 查看伪集群容器列表</span>
<a id="__codelineno-2-2" name="__codelineno-2-2"></a><a href="#__codelineno-2-2"><span class="linenos" data-linenos="2 "></span></a>docker ps
<a id="__codelineno-2-3" name="__codelineno-2-3"></a><a href="#__codelineno-2-3"><span class="linenos" data-linenos="3 "></span></a>
<a id="__codelineno-2-4" name="__codelineno-2-4"></a><a href="#__codelineno-2-4"><span class="linenos" data-linenos="4 "></span></a><span class="c1"># 客户端连接测试</span>
<a id="__codelineno-2-5" name="__codelineno-2-5"></a><a href="#__codelineno-2-5"><span class="linenos" data-linenos="5 "></span></a><span class="nb">export</span> <span class="nv">localIp</span><span class="o">=</span><span class="k">$(</span>ifconfig<span class="p">|</span>grep -A <span class="m">4</span> -E <span class="s1">&#39;^em*|^eno*|^enp*|^ens*|^eth*|^wlp*&#39;</span><span class="p">|</span>grep <span class="s1">&#39;inet&#39;</span><span class="p">|</span>awk <span class="s1">&#39;{print $2}&#39;</span><span class="p">|</span>head -1 <span class="m">2</span>&gt;/dev/null<span class="k">)</span>
<a id="__codelineno-2-6" name="__codelineno-2-6"></a><a href="#__codelineno-2-6"><span class="linenos" data-linenos="6 "></span></a>
<a id="__codelineno-2-7" name="__codelineno-2-7"></a><a href="#__codelineno-2-7"><span class="linenos" data-linenos="7 "></span></a>mysql -h<span class="nv">$localIp</span> -uroot -P3306 -p123456
</code></pre></div>
<ul>
<li>
<p>2.2 配置 MGR 及引导启动</p>
</li>
<li>
<p><a href="https://blogs.wl4g.com/archives/650">同理参见: 基于主机离线部署 MySQL MGR 高可用生产集群#7.1</a>，也与 1.2 类似，只是分布在三台宿主机。</p>
</li>
<li>
<p>或使用工具 <a href="https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/docker-mysql-mgr-simple-deployment/resources/mgrctl.sh">mgrctl</a> 操作
<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1"></a><a href="#__codelineno-3-1"><span class="linenos" data-linenos="1 "></span></a>sudo curl -sSL -o /bin/mgrctl https://gitee.com/wl4g/blogs/raw/master/docs/articles/database/docker-mysql-mgr-simple-deployment/resources/mgrctl.sh
</code></pre></div></p>
</li>
</ul>
<h2 id="3">3. 参考<a class="headerlink" href="#3" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="https://blog.csdn.net/lihuarongaini/article/details/108074449">参考 1: Docker 搭建 MySQL 高可用之 MGR</a></li>
</ul>

              
            </article>
          </div>
        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    <script id="__config" type="application/json">{"base": "../../../..", "features": ["search.suggest", "search.highlight", "navigation.tabs", "navigation.expand", "toc.follow", "toc.integrate"], "search": "../../../../assets/javascripts/workers/search.5e67fbfe.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version.title": "Select version"}, "version": {"default": "latest", "provider": "mike"}}</script>
    
    
      <script src="../../../../assets/javascripts/bundle.c44cc438.min.js"></script>
      
        <script src="../../../../static/js/util.js"></script>
      
    
  </body>
</html>